import scrapy

from ScrapyObject.spiders.utils.url_utils import *

'''
scrapy crawl ydbb -o ydbb.json
https://cheaja-shaiyae.chuv-25y0925.cc/oaoaoa/
'''


class YdbbSpider(scrapy.Spider):
    name = "ydbb"
    # 前缀
    prefix = 'https://cheaja-shaiyae.'
    # 中缀
    website = 'chuv-25y0925'
    # 后缀
    suffix = '.cc/'
    allowed_domains = [website + '.cc']
    start_urls = [prefix + website + suffix + 'oaoaoa/']

    def __init__(self):
        self.i = 0

    def parse(self, response):
        content = get_data(response)
        video_urls = get_video_url_two(content)
        if len(video_urls):
            for video_url in video_urls:
                self.i = self.i + 1
                yield get_video_item(id=self.i, tags='', url=response.url, name='', pUrl='', vUrl=format_url_one(video_url))
        else:
            names = response.xpath("//div[@class='stui-content__detail']//h1/text()").extract()
            temp_tags = response.xpath("//div[@class='stui-content__detail']//p/text()").extract()
            picture_urls = response.xpath("//a[@class='pic']//img/@ data-original").extract()
            urls = response.xpath("//div[@class='play-btn']//a/@ href").extract()
            if len(names) and len(temp_tags) and len(picture_urls) and len(urls):
                tags = re.findall(r'：(.*?)\s*(?:/|$)', temp_tags[0])
                self.i = self.i + 1
                yield get_video_item(id=self.i, tags=tags[0], url=split_joint(self.prefix + self.website + self.suffix, urls[0]), name=names[0], pUrl=picture_urls[0], vUrl="")
        url_list = get_url(content)
        for url in url_list:
            if url.startswith('/'):
                yield scrapy.Request(split_joint(self.prefix + self.website + self.suffix, url), callback=self.parse)
